package com.zkthink.ceres.store.service.impl;

import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.R;
import com.zkthink.base.service.SuperServiceImpl;
import com.zkthink.ceres.common.enums.AdjustStatuseEnum;
import com.zkthink.ceres.member.dao.MemberMapper;
import com.zkthink.ceres.pay.service.PayTransferService;
import com.zkthink.ceres.store.dao.StoreMapper;
import com.zkthink.ceres.store.dao.StoreWithdrawHisMapper;
import com.zkthink.ceres.store.dto.StoreProjectDto;
import com.zkthink.ceres.store.dto.StoreWithdrawHisPageDTO;
import com.zkthink.ceres.store.dto.StoreWithdrawHisSaveDTO;
import com.zkthink.ceres.store.dto.StoreWithdrawHisUpdateDTO;
import com.zkthink.ceres.store.entity.Store;
import com.zkthink.ceres.store.entity.StoreWithdrawHis;
import com.zkthink.ceres.store.service.StoreService;
import com.zkthink.ceres.store.service.StoreWithdrawHisService;
import com.zkthink.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 业务实现类
 * 店铺提现历史
 * </p>
 *
 * @author chenke
 * @date 2022-04-29
 */
@Slf4j
@Service
public class StoreWithdrawHisServiceImpl extends SuperServiceImpl<StoreWithdrawHisMapper, StoreWithdrawHis> implements StoreWithdrawHisService {
   @Autowired
   private StoreMapper storeMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private PayTransferService payTransferService;
    @Override
    public R<IPage<StoreWithdrawHis>> findXsyWithdrawHis(IPage page, StoreWithdrawHisPageDTO model) {
        return R.success(baseMapper.findXsyWithdrawHis(page, model));
    }


    /**
     * 1、先判断是否已经审核过了
     * 2、判断是通过还是拒绝
     * 3、通过的话调用提现接口
     * 3.1 调用接口成功，则插入cashHis
     * 3.2 调用接口失败,则退还giftNum
     * 4、拒绝
     * 4.1退还giftNum
     */
    @Override
    @Transactional
    public R adjustStoreXsyWithdrawHis(StoreWithdrawHisUpdateDTO dto, String ip) {

        Store store = storeMapper.myScore(2,dto.getMid());
        StoreWithdrawHis xsyWithdrawHis = baseMapper.selectById(dto.getId());
        xsyWithdrawHis.setAdjustTime(LocalDateTime.now());
        if (xsyWithdrawHis == null || xsyWithdrawHis.getSupplyStatus() != AdjustStatuseEnum.JXZ_STATUS.getCode() || dto.getSupplyStatus() == AdjustStatuseEnum.JXZ_STATUS.getCode()) {
            return R.fail("不可审核");
        }
        xsyWithdrawHis.setAdjustId(dto.getAdjustId());
        xsyWithdrawHis.setAdjustTime(LocalDateTime.now());
        //拒绝
        if (dto.getSupplyStatus() == AdjustStatuseEnum.NTG_STATUS.getCode()) {
            xsyWithdrawHis.setSupplyStatus(AdjustStatuseEnum.NTG_STATUS.getCode());
            xsyWithdrawHis.setRemark(dto.getRemark());
        } else if (dto.getSupplyStatus() == AdjustStatuseEnum.TG_STATUS.getCode()) {
//            String tradingNo = UUID.randomUUID().toString();
//            String amount = String.valueOf(xsyWithdrawHis.getCash());
//            String payeeAccount = xsyWithdrawHis.getWithdrawAccount();
//            String remark = "余额提现款";
//            String companyName = memberMapper.selectById(dto.getMid()).getPhone();
//            R<AlipayFundTransUniTransferResponse> tranfer = null;
//            xsyWithdrawHis.setWithdrawId(tradingNo);
//            //判断提现到支付宝还是微信
//            if (dto.getAccountType() == 0) {
//                //微信
//                tranfer = payTransferService.wxTranfer(ip, amount, payeeAccount, companyName, remark, tradingNo);
//
//            } else if (dto.getAccountType() == 1) {
//                //支付宝
//                tranfer = payTransferService.aliTranfer(tradingNo, amount, companyName, payeeAccount, remark);
//            }
//            if (tranfer == null || !tranfer.getIsSuccess()) {
//                //失败
//                xsyWithdrawHis.setSupplyStatus(AdjustStatuseEnum.NTG_STATUS.getCode());
//                xsyWithdrawHis.setRemark(tranfer == null ? "退款调用失败" : tranfer.getMsg());
//            } else {
                //成功  修改店铺余额
                store.setTurnover(store.getTurnover().subtract(xsyWithdrawHis.getCash()));
                storeMapper.updateById(store);
                xsyWithdrawHis.setSupplyStatus(AdjustStatuseEnum.TG_STATUS.getCode());
                //修改订单状态
                baseMapper.UpdateChlOederOrderStatus(6,Long.valueOf(xsyWithdrawHis.getWithdrawId()));
            }

         baseMapper.updateById(xsyWithdrawHis);

//        if (xsyWithdrawHis.getSupplyStatus() == AdjustStatuseEnum.TG_STATUS.getCode()) {
//            //退还现金或者小红心
//            if (dto.getWithdrawType() == 0) {
//
//                server.setTurnover(server.getTurnover().add(xsyWithdrawHis.getCash()));
//            }
//            storeMapper.updateById(server);
//        }
        return R.success();
    }
    /**
     * 今日预约
     * @return
     */
    @Override
    public R today(Long storeid) {
        String s = DateUtils.formatAsDate(new Date());
        StoreProjectDto dto = new StoreProjectDto();
        dto.setCount( storeMapper.yuyue(storeid,s));
        dto.setPaysum( storeMapper.today(storeid,s));
        return R.success(dto);
    }

}
